package org.bing.zion.core;

public class MessageHandlerChain {

	private MessageHandlerWrapper first;
	private MessageHandlerWrapper last;
	private MessageHandler messageHandler;

	// TODO impl dynamic filter update, more position setting
	public void addHandler(MessageHandler handler) {
		if (first == null) {
			first = new MessageHandlerWrapper(this, handler);
			last = first;
			return;
		}
		MessageHandlerWrapper wrapper = new MessageHandlerWrapper(this, handler);
		wrapper.setPrevWrapper(last);
		last.setNextWrapper(wrapper);
		last = wrapper;
	}

	public void setHandler(MessageHandler messageHandler) {
		this.messageHandler = messageHandler;
	}

	public MessageHandler getHandler() {
		return messageHandler;
	}

	public MessageHandlerWrapper first() {
		if (first != null) {
			return first;
		} else {
			return new MessageHandlerWrapper(this, null);
		}
	}

	public MessageHandlerWrapper last() {
		if (last != null) {
			return last;
		} else {
			return new MessageHandlerWrapper(this, null);
		}
	}

	// NOTICE impl by subclass, ProtocolFilterWrapper
	public MessageHandler next() {
		return null;
	}

	public MessageHandler prev() {
		return null;
	}

}
